import { useUserManagerStore } from './userManagerStore'
import { useUserStore } from './userStore'
import { ApiEndpoints } from '~/types/ApiEndpoints'
import { IUser } from '~/types/User'

interface IState {
  favoritesList: IUser[]
  favoritesListPending: boolean
}

export const useFavoritesStore = defineStore('favoritesStore', {
  state: () =>
    ({
      favoritesList: [],
      favoritesListPending: false
    } as IState),
  actions: {
    async getFavoritesList(filter: any) {
      const config = useRuntimeConfig()
      const userManagerStore = useUserManagerStore()
      const userStore = useUserStore()
      this.favoritesListPending = true
      this.favoritesList = await $fetch(ApiEndpoints.GET_FAVORITES, {
        method: 'POST',
        baseURL: config.public.baseURL,
        headers: [
          [
            'Authorization',
            'Bearer ' + (userManagerStore.user?.access_token || '')
          ]
        ],
        body: {
          favorites: userStore.user.favorites,
          filter: { ...filter }
        }
      })
      this.favoritesListPending = false
    }
  }
})
